//
//  ViewController.m
//  XYG播放器Demo
//
//  Created by 夏阳光 on 16/5/3.
//  Copyright © 2016年 夏阳光. All rights reserved.
//

#import "ViewController.h"
#import "HomeTableViewCell.h"
#import "HomeModel.h"
#import "VideoViewController.h"

@interface ViewController ()
{
    NSMutableArray *_videoArray;
}
@property (strong, nonatomic) AFHTTPSessionManager *manager;

@property (weak, nonatomic) IBOutlet UITableView *HomeTableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"视频";
    
    _videoArray=[[NSMutableArray alloc]init];
    
    
    
    [self loadNewData];
}

#pragma mark - tableview delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _videoArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTableViewCell" forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    HomeModel *model=_videoArray[indexPath.row];
    [cell.backgroundImage sd_setImageWithURL:[NSURL URLWithString:model.bg_img]];
    cell.titleLable.text=model.title;
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    

}


#pragma mark - 网络请求
-(AFHTTPSessionManager *)manager
{
    if (!_manager)
    {
        self.manager = [[AFHTTPSessionManager alloc] init];
    }
    
    return _manager;
}

-(void)loadNewData
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"99F8B523-3E0F-4FAD-8656-F12BD7B60ACA" forKey:@"device"];
    [params setObject:@"" forKey:@"login_token"];
    [params setObject:@"" forKey:@"mem_id"];
    [params setObject:@"ios" forKey:@"native_type"];
    [params setObject:@"c237c9bdba69fe466ee7f0f0be91077d" forKey:@"safe_code"];
    [params setObject:@"2.0" forKey:@"version"];
    
    
    self.manager.responseSerializer.acceptableContentTypes=[self.manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    
    [self.manager POST:@"http://115.28.4.108/api.php?m=home" parameters:params progress:^(NSProgress * _Nonnull uploadProgress)
     {
         
         
     } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         
         NSLog(@"%@",responseObject);
         
         for (NSDictionary *dic in responseObject[@"data"][@"meiri"])
         {
             HomeModel *model=[[HomeModel alloc]initWithDictionary:dic error:nil];
             [_videoArray addObject:model];
         }
         
         [self.HomeTableView reloadData];
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         
         NSLog(@"%@--",error);
         
     }];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    VideoViewController *vc=segue.destinationViewController;
    vc.dataArr=_videoArray;
    
    NSIndexPath* indexPath=[self.HomeTableView indexPathForSelectedRow];
    HomeModel *model=_videoArray[indexPath.row];
    vc.title=model.title;
    vc.index=indexPath.row;
}



@end
